package vn.edu.hcmut.cse.ai.search.problems.npuzzle;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author ngphi
 */
public class ReadInput {

    private int size;
    private int[][] start;
    private int[][] goal;
    public void handle(String filename) {
        List<String> rowString = new ArrayList<>();

        Path path = Paths.get(filename);
        try {
            rowString = Files.readAllLines(path, StandardCharsets.UTF_8);
        } catch (IOException e) {
            System.out.println("Error File");
        }
        this.size = (rowString.size() - 1) / 2;
        this.start = new int[size][size];
        this.goal = new int[size][size];
        boolean checkStart = true;
        boolean checkGoal = false;
        for (int i = 0; i < rowString.size(); i++) {
            if (rowString.get(i).length() == 0) {
                i++;
                checkStart = false;
                checkGoal = true;
            }
            if (checkStart) {
                String str = rowString.get(i);
                str = str.replaceAll("[^0-9]+", " ");
                List arr = Arrays.asList(str.trim().split(" "));
                for (int j = 0; j < arr.size(); j++) {
                    this.start[i][j] = Integer.valueOf(arr.get(j).toString());
                }
            }
            if (checkGoal) {
                String str = rowString.get(i);
                str = str.replaceAll("[^0-9]+", " ");
                List arr = Arrays.asList(str.trim().split(" "));
                for (int j = 0; j < arr.size(); j++) {
                    this.goal[i - this.size - 1][j] = Integer.valueOf(arr.get(j).toString());
                }
            }
        }
    }

    public int getSize() {
        return size;
    }

    public int[][] getStart() {
        return start;
    }

    public int[][] getGoal() {
        return goal;
    }
}
